package mdv.exp.items.list;

import android.app.Activity;
import android.app.Dialog;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.data.MyDataBaseHelper;

public class ItemsListAdapter extends BaseAdapter implements OnClickListener{

	private static final String TAG = "MainListAdapter";
	private Activity activity;
	private Cursor cursor;
	private MyDataBaseHelper db;

	public ItemsListAdapter(Activity activity, Cursor c, boolean autoRequery) {
		super();
		this.activity = activity;
		 //test to be deleted:
        db = new MyDataBaseHelper(activity);
        db.openDataBase();
        cursor = db.queryAll();
        cursor.moveToFirst();
        
	}

	
	@Override

	public View getView(int position, View convertView, ViewGroup parent){
		ItemsListRowView view;
		if(convertView == null){
			ItemsListRowView row = new ItemsListRowView(activity);
			view = row;
			view.setOnClickListener(this);
		}else
			view = (ItemsListRowView)convertView;
		//Log.d(TAG,"getViw: " + position);
		//cursor.move(position);
		cursor.moveToPosition(position);
		view.setData(new ItemsListRowData(cursor));
		
		return view;
		
	}
	
	@Override
	public int getCount(){
		return cursor.getCount() - 1;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}


	@Override
	public void onClick(View v) {
		ItemsListRowView row = (ItemsListRowView)v;
		Dialog dialog = new Dialog(activity);
		dialog.setTitle("Row N: " + row.getId());
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		
	}


	public void close() {
		db.close();
		
	}
	
	

}
